package com.woniu.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.finance.entity.Tender;
import com.woniu.finance.entity.vo.TenderVO;
import com.woniu.finance.mapper.TenderMapper;
import com.woniu.platform.entity.PlatformTenderAuditRecord;
import com.woniu.platform.mapper.PlatformTenderAuditRecordMapper;
import com.woniu.platform.service.PlatformTenderService;
import com.woniu.util.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@Service
public class PlatformTenderServiceImpl implements PlatformTenderService {
    @Autowired
    private PlatformTenderAuditRecordMapper platformTenderAuditRecordMapper;
    @Autowired
    private TenderMapper tenderMapper;

    /**
     * 通过多种属性查找标的
     * @return
     */
    @Override
    public HttpResult<?> selectTender(TenderVO tenderVO) {
        try {
            List<Tender> tenders = tenderMapper.selectTender(tenderVO);
            return new HttpResult<>().ok(tenders);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    /**
     * 查找发标待审核标的
     * @return
     */
    @Override
    public HttpResult<?> selectTenderStart(TenderVO tenderVO) {
        tenderVO.setTenderStage(0);
        tenderVO.setCoreAuditResults(1);
        try {
            List<Tender> tenders = tenderMapper.selectTender(tenderVO);
            return new HttpResult<>().ok(tenders);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    /**
     * 查找流标标的
     * @return
     */
    @Override
    public HttpResult<?> selectTenderFlow(TenderVO tenderVO) {
        tenderVO.setTenderStage(1);
        try {
            List<Tender> tenders = tenderMapper.selectTender(tenderVO);
            return new HttpResult<>().ok(tenders);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    /**
     * 查找满标待审核标的
     * @return
     */
    @Override
    public HttpResult<?> selectTenderFull(TenderVO tenderVO) {
        try {
            tenderVO.setTenderStage(1);
            List<Tender> tenders = tenderMapper.selectTender(tenderVO);
            Iterator<Tender> iterator = tenders.iterator();
            while (iterator.hasNext()){
                Tender next = iterator.next();
                if(next.getAllFinancingAmount().equals(next.getNowFinancingAmount())){
                    iterator.remove();
                }
            }
            return new HttpResult<>().ok(tenders);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    /**
     * 标第审核（招标审核，满标审核）
     * @param result
     * @return
     */
    @Override
    public HttpResult<?> TenderAudit(Integer result,Integer tenderId) {
        try {
            Tender tender = tenderMapper.selectById(tenderId);
            tender.setPlatformAuditResults(result);
            PlatformTenderAuditRecord platformTenderAuditRecord=new PlatformTenderAuditRecord();
            platformTenderAuditRecord.setTenderId(tenderId);//标的id
            platformTenderAuditRecord.setAuditStage(tender.getTenderStage()+1);//
            platformTenderAuditRecord.setAuditResult(result);
            platformTenderAuditRecordMapper.insert(platformTenderAuditRecord);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }
}
